package com.weishenai.customerassistant.controller.sys;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.map.MapUtil;
import com.google.code.kaptcha.Producer;
import com.weishenai.customerassistant.common.Const;
import com.weishenai.customerassistant.common.Result;
import com.weishenai.customerassistant.common.dto.SysProfileInfoDto;
import com.weishenai.customerassistant.controller.BaseController;
import com.weishenai.customerassistant.entity.sys.SysUserEntity;
import com.weishenai.customerassistant.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.security.Principal;
import java.util.Base64;

/**
 * @Auther: weishenai
 * @Date: 2021/11/27/13:33
 * @Description: 生成验证码的方法
 */
@Api(tags = "管理系统 登录权限")
@Slf4j
@RestController
public class AuthController extends BaseController {
    @Autowired
    private Producer producer;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private Environment environment;

    /**
     * 图片验证码
     */
    @ApiOperation("获取验证码")
    @GetMapping("/captcha")
    public Result captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String code = producer.createText();
        String key = UUID.randomUUID().toString();
        BufferedImage image = producer.createImage(code);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", outputStream);
//        BASE64Encoder encoder = new BASE64Encoder();

        String str = "data:image/jpeg;base64,";
        String base64Img = str + Base64.getEncoder().encodeToString(outputStream.toByteArray());
        //存储到redis中
        redisUtils.hset(Const.CAPTCHA_KEY, key, code, 120);
        log.info("验证码 -- {} -- {}", key, code);
        return Result.success(MapUtil.builder().put("token", key).put("captchaImg", base64Img).build());
    }

    @ApiOperation("获取登录信息")
    @GetMapping("sys/userInfo")
    public Result getUserInfo(Principal principal) {
        SysUserEntity sysUser = sysUserService.getByUsername(principal.getName());

        SysProfileInfoDto sysProfileInfoDto = new SysProfileInfoDto();
        sysProfileInfoDto.setId(sysUser.getId());
        sysProfileInfoDto.setUsername(sysUser.getUsername());
        sysProfileInfoDto.setAvatar(sysUser.getAvatar());
        sysProfileInfoDto.setEmail(sysUser.getEmail());
        sysProfileInfoDto.setCity(sysUser.getCity());
        sysProfileInfoDto.setSubstation(sysUser.getSubstation());
        return Result.success(sysProfileInfoDto);
    }

    @ApiOperation("获取服务器Url")
    @PostMapping("/getUrl")
    public Result getUrl() throws UnknownHostException {
        String port = String.valueOf(environment.getProperty("server.port"));
        String url = null;
        try {
            url = Inet4Address.getLocalHost().getHostAddress();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success(MapUtil.builder().put("port", port).put("url", url).build());
    }
}
